package com.manage.controller;

import com.manage.pojo.Collections;
import com.manage.pojo.operation.CollectionsList;
import com.manage.service.CollectionsService;
import com.manage.utils.FBJSONResult;
import com.manage.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "有关收藏的操作", tags = {"有关收藏的操作"})
public class CollectionsController {

    @Autowired
    private CollectionsService collectionsService;

    @ApiOperation(value = "鞋收藏添加", notes = "鞋收藏添加接口")
    @PostMapping("/collection/collectionRegist")
    public FBJSONResult collectionRegist(@RequestBody Collections collection) throws Exception {
        collection.setUserId(collection.getUserId());
        collection.setShoesId(collection.getShoesId());
        // 日期格式为yyyy-MM-dd HH:mm:ss String类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        collection.setCreateTime(sdf.format(new Date()));
        collectionsService.saveCollections(collection);
        return FBJSONResult.ok(collection);
    }

    @ApiOperation(value = "鞋收藏删除", notes = "鞋收藏删除接口")
    @ApiImplicitParam(name = "collectionId", value = "鞋收藏id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/collection/collectionDelete")
    public FBJSONResult collectionDelete(String collectionId) throws Exception {
        if(StringUtils.isBlank(collectionId)) {
            return FBJSONResult.errorMsg("鞋收藏id不能为空");
        }
        boolean collectionIdIsExit = collectionsService.queryCollectionsIsExit(collectionId);
        if(collectionIdIsExit) {
            collectionsService.collectionDelete(collectionId);
        }
        return FBJSONResult.ok("删除成功");
    }

    @ApiOperation(value = "鞋收藏信息查询", notes = "鞋收藏信息查询接口")
    @ApiImplicitParam(name = "collectionId", value = "图书收藏id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/collection/queryCollectionInfoByCollectionId")
    public FBJSONResult queryCollectionInfoByCollectionId(String collectionId) throws Exception {
        if(StringUtils.isBlank(collectionId)) {
            return FBJSONResult.errorMsg("鞋收藏id不能为空");
        }
        CollectionsList collectionResult = collectionsService.queryCollectionsInfoByCollectionId(collectionId);
        return FBJSONResult.ok(collectionResult);
    }

    @ApiOperation(value = "鞋收藏列表信息查询(根据用户id)", notes = "鞋收藏信息列表查询(根据用户id)接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/collection/queryCollectionInfoByUserId")
    public FBJSONResult queryCollectionInfoByUserId(Integer page, String userId) throws Exception {
        if(StringUtils.isBlank(userId)) {
            return FBJSONResult.errorMsg("用户id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = collectionsService.queryCollectionsInfoByUserId(page, 10, userId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "鞋收藏列表信息查询(根据鞋id)", notes = "鞋收藏信息列表查询(根据鞋id)接口")
    @ApiImplicitParam(name = "shoesId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/collection/queryCollectionInfoByShoesId")
    public FBJSONResult queryCollectionInfoByShoesId(Integer page, String shoesId) throws Exception {
        if(StringUtils.isBlank(shoesId)) {
            return FBJSONResult.errorMsg("鞋id不能为空");
        }
        if(page == null) {
            page = 1;
        }
        PagedResult result = collectionsService.queryCollectionsInfoByShoesId(page, 10, shoesId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "鞋收藏列表信息查询(所有)", notes = "鞋收藏信息列表查询(所有)接口")
    @PostMapping("/collection/queryAllCollectionInfo")
    public FBJSONResult queryAllCollectionInfo(Integer page) throws Exception {
        if(page == null) {
            page = 1;
        }
        PagedResult result = collectionsService.queryCollections(page, 10);
        return FBJSONResult.ok(result);
    }

}
